Subject: Re: Variable Length Structures ?
From: [email-masked] (Michael J Zehr)
Date: 22 Mar 1994 15:36:27 GMT
Newsgroups: comp.lang.c

In article <ua302aa.764269592@sun2> [email-masked] (Kurt Watzka) writes:
>[email-masked] (derek.a.dewar) writes:
>>Does anyone out there in C-land know if it's at all possible to have 
>>a variable length structures in C ? Something like the following :
>Try something like this:
>   struct blah {
>      /* fixed fields */
>      int num_fields;
>      int var[ 1 ];
>   }
>
>Then allocate memory like this 
>   struct blah *p = (struct blah *)
>      malloc( sizeof( struct blah ) +
>              (num_fields - 1 ) * sizeof( int ) );
>You can't successfully use sizeof on such a "variable length
>structure". [or assignment]

The C standards committee recently ruled that this is not strictly
conforming and not guaranteed to work.  While it will work on most
platforms, one should be aware of this when using it.  

Naturally a C vendor is free to provide this as a language extension,
but if they don't *say* they provide it, then they don't have to.  (No,
I'm not too happy about the situation either.)

[The reasoning behind the restriction has to do with compilers that
might do bounds checking based on the declared size of the array.  One
can also imagine segmented architectures for which the size of the
struct is just below the size of a segment, but the extra space makes it
larger than a segment, yet accesses to the array are generated by the
compiler assuming that the array is in the same segment as the rest of
the structure.  I'm not claiming there *are* such architectures, only
that there could be.]

*sigh*

-michael j zehr